package Muilticast;



import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.util.Date;
import java.util.Random;

import twitter.Server.ServerType;
import twitter.TwitterLibrary.SocketConfig;
import twitter.TwitterLibrary.Utility;

import Twitter.RequestSender.RequestSender;

public class MulticastNoticeSender  implements Runnable{
	private MulticastSocket s;
	//private Random r;
	private Utility utility;
	public MulticastNoticeSender(Utility utility)
	{
		
		this.utility=utility;
		
	}
	@Override
	
	public void run() {
		// TODO Auto-generated method stub
		  for(int i=0;i<10;i++) {
		        try {
		        	//System.out.println("broadcast one message");
		        	// Which port should we send to
		    		int port = 9991;
		    		// Which address
		    		String groupname = "230.0.0.1";
		    		// Which ttl
		    		int ttl = 1;
		    		String dString="";
		    		if(SocketConfig.serverType==ServerType.PrimaryServer)
		    		{
		    			 dString=utility.constructNotice(true);
		    		}
		    		else
		    		{
		    			 dString=utility.constructNotice(false);
		    		}
		    		
		    		
	    		
		    		InetAddress group = InetAddress.getByName(
		    				groupname);
		    			s = new MulticastSocket();
		    			// Note that we don't have to join the multicast group if we are only
		    			// sending data and not receiving

		    			

		    			// Fill the buffer with some data
		    			byte buf[] =new  byte[256];
		    			 buf = dString.getBytes();

		    			// Create a DatagramPacket 
		    			DatagramPacket pack = new DatagramPacket(buf, buf.length,
		    					group, port);
		    			// Do a send. Note that send takes a byte for the ttl and not an int.
		    			s.send(pack);
		    			
		        } catch (IOException e) {
		            e.printStackTrace();
		            
		        }

		        s.close();
		    }
		    
	}
	

}
